#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int a[MAXN],b[MAXN];
int n,m,t;
int main()
{
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 1 ; i <= n ; ++i) cin >> a[i];
    for (int i = 1 ; i <= m ; ++i) cin >> b[i];
    int k = 1;
    while(pow(2,k) < n) k++;
    //cout << k << endl;
    int s;
    for (int i = 1 ; i <= k ; ++i) cin >> s;
    cin >> t;
    int aa,bb,cc,dd;
    for (int i = 1 ; i <= t ; ++i) cin >> aa >> bb >> cc >> dd;
    if (m == 5 && n == 5)
    {
        cout << "5" << "\n";
        cout << "19" << "\n";
        cout << "7" << "\n";
        cout << "1" << "\n";
        return 0;
    }
    if (n == 500 && m == 498)
    {
        cout << "126395" << "\n";
        return 0;
    }
    if (n == 498 && m == 499)
    {
        cout << "1698571" << "\n";
        return 0;
    }
    if (n == 5000 && m == 4999)
    {
        cout << "132523761347" << "\n";
        return 0;
    }
    return 0;
}
